package fi.jamk.ourcookingapp.backend.architecture.services;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import fi.jamk.ourcookingapp.backend.architecture.interfaces.IReceiptManager;
import fi.jamk.ourcookingapp.backend.architecture.manager.ReceiptManager;
import fi.jamk.ourcookingapp.backend.exceptions.NoIngredientsException;
import fi.jamk.ourcookingapp.backend.exceptions.NoReceiptException;
import fi.jamk.ourcookingapp.backend.exceptions.NoResultException;
import fi.jamk.ourcookingapp.backend.exceptions.NoStepsException;
import fi.jamk.ourcookingapp.backend.model.Ingredient;
import fi.jamk.ourcookingapp.backend.model.Rating;
import fi.jamk.ourcookingapp.backend.model.Receipt;
import fi.jamk.ourcookingapp.backend.model.Step;
import fi.jamk.ourcookingapp.backend.model.User;

public class ReceiptServices implements IReceiptManager{

	private IReceiptManager rManager;
	
	public ReceiptServices() {
		this.rManager = new ReceiptManager();
	}
	
	public void setFilter(String key, Object value){
		this.rManager.setFilter(key, value);
	}
	
	public void setOption(String key, String value){
		this.rManager.setOption(key, value);
	}
	
	public void addToFavourite(Receipt receipt, User user) throws JSONException, InterruptedException, ExecutionException{
		this.rManager.addToFavourite(receipt, user);
	}
	
	public boolean deleteReceipt(Receipt receipt) throws InterruptedException, ExecutionException, JSONException {
		return this.rManager.deleteReceipt(receipt);
	}

	public Receipt updateReceipt(Receipt receipt, boolean clicks) throws InterruptedException, ExecutionException, JSONException {
		return this.rManager.updateReceipt(receipt, clicks);
	}

	public Receipt createReceipt(Receipt receipt) throws NoIngredientsException, NoStepsException, InterruptedException, ExecutionException, JSONException {
		if(receipt.getLstIngredient().isEmpty()){
			throw new NoIngredientsException();
		}
		if(receipt.getLstStep().isEmpty()){
			throw new NoStepsException();
		}
		return this.rManager.createReceipt(receipt);
	}

	public List<Receipt> filter(boolean count) throws InterruptedException, ExecutionException, JSONException, NoReceiptException {
		return this.rManager.filter(count);
	}

	public double rateReceipt(Receipt receipt, Rating rating) throws InterruptedException, ExecutionException, NumberFormatException, JSONException {
		return this.rManager.rateReceipt(receipt, rating);
	}

	public List<Ingredient> getAllIngredients() throws NoResultException, JSONException, InterruptedException, ExecutionException {
		List<Ingredient> allIngredients = this.rManager.getAllIngredients();
		if(allIngredients.isEmpty()){
			throw new NoResultException("getAllIngredients");
		}
		return allIngredients;
	}

	public List<Receipt> getFavourites(User user) throws JSONException, InterruptedException, ExecutionException {
		return this.rManager.getFavourites(user);
	}

	public List<Ingredient> ingredientsByReceipt(Receipt receipt) throws JSONException, InterruptedException, ExecutionException {
		return this.rManager.ingredientsByReceipt(receipt);
	}

	public List<Step> stepsByReceipt(Receipt receipt) throws JSONException, InterruptedException, ExecutionException {
		return this.rManager.stepsByReceipt(receipt);
	}

	public void removeFromFavourite(Receipt receipt, User user) throws JSONException, InterruptedException, ExecutionException {
		this.rManager.removeFromFavourite(receipt, user);
	}

}
